package com.douxg.fastCoding.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douxg.fastCoding.db.Config;
import com.douxg.fastCoding.spring.util.PageUtil;
import com.douxg.fastCoding.spring.vo.Result;
import com.douxg.fastCoding.web.dto.ProjectDv;
import com.douxg.fastCoding.web.entity.Project;
import com.douxg.fastCoding.web.service.ProjectService;
import com.douxg.fastCoding.web.vo.ElSelectVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/6 14:30
 */
@RequestMapping("fc/project")
@RestController
public class ProjectController {
    @Resource
    private ProjectService service;

    @PostMapping("add")
    public Result add(@RequestBody Project project) {
        LocalDateTime now = LocalDateTime.now();
        project.setUpdateTime(now);
        project.setCreateTime(now);
        boolean handleResult = service.save(project);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("modify")
    public Result modify(@RequestBody Project project) {
        LocalDateTime now = LocalDateTime.now();
        project.setCreateTime(null);
        project.setUpdateTime(now);
        boolean handleResult = service.updateById(project);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    public Result delete(@RequestBody Project project) {
        boolean handleResult = service.removeById(project.getId());
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("page")
    public Result page(@RequestBody ProjectDv projectDv, Integer current, Integer size) {
        Page<Project> page = new Page<>();
        page.setCurrent(PageUtil.handlePageIndex(current));
        page.setSize(PageUtil.handlePageSize(size));
        page.setSearchCount(true);
        Page<Project> projectPage = service.page(page, projectDv);
        return Result.success(projectPage);
    }

    @PostMapping("config")
    public Result config(@RequestBody Config config, Long id) {
        LocalDateTime now = LocalDateTime.now();
        Project project = new Project();
        project.setId(id);
        project.setUpdateTime(now);
        String configStr = JSON.toJSONString(config);
        project.setConfig(configStr);

        boolean handleResult = service.updateById(project);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("names")
    public Result names() {
        List<Project> projects = service.list();
        List<ElSelectVo> list = new ArrayList<>();
        for (Project project : projects) {
            ElSelectVo elSelectVo = ElSelectVo.createByValueAndLabel(project.getId() + "", project.getName());
            list.add(elSelectVo);
        }
        return Result.success(list);
    }
}
